<?php

if (!defined("BASEPATH"))
    exit("No direct script access allowed");
class works extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library("table");
        $this->load->helper(array(
            "file",
            "form",
            "html"));
        $this->load->model("home_model");
        $this->load->model("works_model");
    }

    public function sort($field = "created", $order = "desc")
    {
        $this->works_model->dataSort($field, $order);
        $this->index();
    }

    public function index($target = 0)
    {
        $arr = $this->works_model->dataIndex($target);
        $breakCrum = '<a href="' . base_url() . '">' . t("Home") . '</a> » <span>' . t("works") .
            '</span>';
        $data = array("content" => $this->load->view("works/main", $arr, true),
                "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    }

    public function find()
    {
        if (!isset($_POST["txt_find"]))
            return redirect(site_url("works/index"));
        if (isset($_POST["btnReset"]) && $_POST["btnReset"] == "Reset")
            $this->session->unset_userdata("sm_find_works");
        else {
            $keyword = $this->input->post("txt_find", true);
            if ($keyword != "") {
                notify_set_messages(notifi_info(t("Kết quả tìm kiếm") . " " . $keyword));
                $this->session->set_userdata("sm_find_works", $keyword);
            }
        }

        redirect(site_url("works/index"));
    }
    public function add()
    {
        $arr = $this->works_model->dataAdd();
        $breakCrum = '<a href="' . base_url() . '">' . t("Home") . '</a> » 
    <a href="/admin/works">' . t("works") . '</a> » <span>Add</span>';
        $data = array("content" => $this->load->view("works/add", $arr, true),
                "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    }

    public function edit($id)
    {
        $arr = $this->works_model->dataEdit($id);
        $breakCrum = '<a href="' . base_url() . '">' . t("Home") . '</a> » 
    <a href="/admin/works">' . t("works") . '</a> » <span>Edit</span>';
        $data = array("content" => $this->load->view("works/edit", $arr, true),
                "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    }

    public function doAdd()
    {
        if (!$this->works_model->checkForm()) {
            notify_set_messages(notifi_error(validation_errors()));
            return $this->add();
        }
        if ($this->works_model->doUpdate()) {
            return redirect(site_url("works"));
        }

    }
    public function doEdit()
    {
        if (!$this->works_model->checkForm()) {
            notify_set_messages(notifi_error(validation_errors()));
            return $this->edit(decrypt($_POST["idx"]));
        }
        if ($this->works_model->doUpdate()) {
            redirect(site_url("works"));
        }

    }
    public function del($id)
    {
        $this->db->delete("works", array("id" => $id));
        redirect(site_url("works"));
    }

    public function comp($id)
    {
        echo $this->works_model->updatecomp($id);
    }

    public function ajaxpopup($id = 0)
    {
        $this->load->view('works/ajax_popup');
    }

    public function status_popup()
    {
        $this->load->view('works/status_popup');
    }

}
